<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--      <el-card class="box-card">-->
      <!-- 查询表单 -->
      <content-block>
        <el-form :inline="true" :model="queryForm" class="demo-form-inline">
          <el-form-item label="人员编号">
            <person-rpc ref="personRpc" @personRpcCallBack="personRpcCallBack" />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="queryForm.aac003" placeholder="请输入姓名" :disabled="true" />
          </el-form-item>

          <el-form-item label="业务类型" prop="aaa121">
            <el-select v-model="queryForm.aaa121" placeholder="请选择">
              <el-option
                v-for="item in filteredOptions"
                :key="item.id"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>

          <el-form-item style="float: right">
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </content-block>
      <!--表格渲染-->
      <content-block>
        <el-tabs v-model="tabActive" @tab-click="fnChange">
          <el-tab-pane label="已办业务" name="tab1">
            <el-table ref="table" v-loading="crud.loading" :height="550" :data="crud.data" size="small" style="width: 100%;" border :header-cell-style="{background:'#f5f7fa', color:'#777'}">
              <el-table-column prop="aac001" label="个人编号" fixed="left" width="220px" align="center" header-align="center" />
              <el-table-column prop="aac003" label="姓名" width="150px" align="center" header-align="center" />
              <el-table-column prop="aaa121" label="业务类型" width="250px" align="center" header-align="center" :formatter="formatter" />
              <el-table-column prop="aae719" label="金额" width="200px" align="center" header-align="center" />
              <!--              <el-table-column prop="aab001" label="单位编号" width="150px" align="center" header-align="center" />-->
              <!--              <el-table-column prop="aab004" label="单位名称" width="180px" align="center" header-align="center" />-->
              <el-table-column prop="aae013" label="备注" width="200px" show-overflow-tooltip align="center" header-align="center" />
              <el-table-column prop="aaz002" label="业务日志id" width="250px" align="center" header-align="center" />
              <el-table-column prop="aae011" label="经办人" width="120px" align="center" header-align="center" />
              <el-table-column prop="aae036" label="经办时间" width="200px" align="center" header-align="center" />
              <el-table-column
                label="操作"
                width="180"
                fixed="right"
                align="center"
              >
                <template slot-scope="scope">
                  <!--              <el-button-->
                  <!--                size="mini"-->
                  <!--                type="primary"-->
                  <!--                @click="handlePreview(scope.row)"-->
                  <!--              >预览</el-button>-->
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.row)"
                  >撤销</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination />
          </el-tab-pane>
          <el-tab-pane label="已撤销业务" name="tab2">
            <el-table ref="table" v-loading="crud.loading" :height="550" :data="crud.data" size="small" style="width: 100%;" border :header-cell-style="{background:'#f5f7fa', color:'#777'}">
              <el-table-column prop="aac001" label="个人编号" fixed="left" width="220px" align="center" header-align="center" />
              <el-table-column prop="aac003" label="姓名" width="150px" align="center" header-align="center" />
              <el-table-column prop="aaa121" label="业务类型" width="250px" align="center" header-align="center" :formatter="formatter" />
              <!--              <el-table-column prop="aab001" label="单位编号" width="150px" align="center" header-align="center" />-->
              <!--              <el-table-column prop="aab004" label="单位名称" width="180px" align="center" header-align="center" />-->
              <el-table-column prop="aae017" label="撤销原因" width="200px" show-overflow-tooltip align="center" header-align="center" />
              <el-table-column prop="aae013" label="备注" width="200px" show-overflow-tooltip align="center" header-align="center" />
              <el-table-column prop="aaz002" label="业务日志id" width="250px" align="center" header-align="center" />
              <el-table-column prop="aae011" label="经办人" width="120px" align="center" header-align="center" />
              <el-table-column prop="aae036" label="经办时间" width="200px" align="center" header-align="center" />
              <el-table-column prop="aae018" label="撤销人" width="120px" align="center" header-align="center" />
              <el-table-column prop="aae019" label="撤销时间" width="180px" align="center" header-align="center" />
            </el-table>
            <!--分页组件-->
            <pagination />
          </el-tab-pane>
        </el-tabs>
      </content-block>
    </div>
  </div>
</template>

<script>
import ContentBlock from '@/components/labers/contentBlock'
import CRUD, { crud, header, presenter } from '@crud/crud'
import businessRevoke from '@/views/ynsb_comm/businessrevoke/businessRevoke'
import pagination from '@crud/Pagination'
import PersonRpc from '@/views/ynsb_comm/comm/module/personRpc'
import moment from 'moment'
import commUtil from '@/views/ynsb_comm/comm/util/commUtil'

export default {
  name: 'BusinessRevoke',
  components: {
    PersonRpc,
    ContentBlock,
    pagination
  },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({ title: '业务撤销', url: '/api/businessRevoke/list', idField: 'aae036', sort: 'aae036,desc', crudMethod: { ...businessRevoke }})
  },
  // 数据字典
  dicts: ['aaa121'],
  data() {
    return {
      queryForm: {
        aac001: '',
        aac003: '',
        aaa121: ''
      },
      materialList: [],
      loading: false,
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      addDialogVisible: false,
      previewDialogVisible: false,
      tabActive: 'tab1',
      aae016: '',
      currentMaterial: null,
      allowedValues: ['"040101"', '040102', '030101']
    }
  },
  computed: {
    filteredOptions() {
      // 在这里定义您的过滤条件
      return this.dict.aaa121.filter(item =>
        item.value === '040101' || item.value === '040102' || item.value === '030101'
      )
    }
  },
  methods: {
    moment,
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      if (commUtil.isNull(this.aae016)) {
        this.crud.params = {
          aae016: '1'
        }
      }
      return true
    },
    filterAaa121(query) {
      console.log(this.dict.aaa121, 'this.dict.aaa121222')
      setTimeout(() => {
        const list = this.dict.aaa121
        list.forEach(item => {
          console.log('11111111111111')
        })
      })
    },
    personRpcCallBack(item) {
      this.queryForm.aac001 = item.aac001
      this.queryForm.aac003 = item.aac003
    },
    // 查询材料列表
    fetchMaterialList() {
      if (this.tabActive === 'tab1') {
        this.aae016 = '1'
      } else {
        this.aae016 = '4'
      }
      this.crud.params = {
        aac001: this.queryForm.aac001,
        aae016: this.aae016,
        aaa121: this.queryForm.aaa121
      }
      this.crud.pageChangeHandler(1)
    },
    // 处理查询
    handleQuery() {
      this.previewDialogVisible = false
      this.addDialogVisible = false
      this.fetchMaterialList()
    },
    // tab改变时
    fnChange() {
      this.fetchMaterialList()
    },
    // 重置查询
    resetQuery() {
      this.queryForm = {
        aac001: '',
        aaa121: ''
      }
      this.$refs.personRpc.restAac001('')
      this.materialList()
    },

    // 分页大小变化
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchMaterialList()
    },

    // 当前页变化
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.fetchMaterialList()
    },

    // 显示新增对话框
    showAddDialog() {
      this.addDialogVisible = true
    },

    handleDelete(row) {
      this.$prompt('', '请输入撤销原因', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{1,100}$/,
        inputErrorMessage: '撤销原因不能为空',
        center: true
      }).then(({ value }) => {
        try {
          console.log(value, '撤销原因')
          row.reason = value
          businessRevoke.toRevoke({ ...row }).then(res => {
            this.$message.success('撤销成功!')
            this.fetchMaterialList()
          })
        } catch (error) {
          // 4. 错误处理
          this.$message.error(`删除失败: ${error.message || '未知错误'}`)
        }
      }).catch(() => {
        // 用户点击取消
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration: 1000
        })
      })
    },

    // 刷新列表
    handleRefresh() {
      this.fetchMaterialList()
    },
    // 关闭新增
    gbCallBack() {
      this.previewDialogVisible = false
      this.addDialogVisible = false
    },
    uploadSuccessBack() {
      this.addDialogVisible = false
      this.fetchMaterialList()
    },
    formatter(row, column, cellValue) {
      // 根据 aae140 的值进行转码
      const key = column.property
      const data = this.dict[key]
      if (data.length === 0) {
        return cellValue
      }
      const dataList = data.filter(item => item.value === cellValue)
      console.log(dataList, 'data')
      if (dataList !== null && dataList !== [] && dataList !== undefined && dataList.length > 0) {
        return dataList[0].label
      } else {
        return cellValue
      }
    }
  }
}
</script>

<style scoped>
.material-management {
  padding: 20px;
}

.box-card {
  max-width: 1200px;
  margin: 0 auto;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>
